/**
 * 
 */
package org.vsg.stock.dzh;

import java.util.List;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.vsg.stock.basic.domain.StockInfo;
import org.vsg.stock.basic.Stock;
import org.vsg.stock.basic.persistence.StockInfoDao;
import org.vsg.stock.basic.service.StockService;


/**
 * @author vison
 *
 */
public class UpdateStockInfoCapital {

	private final static Logger logger = LoggerFactory.getLogger(UpdateStockInfoCapital.class);	
	
	
	public void exec(ApplicationContext appContext) throws Exception {
		
		StockService stkService = (StockService)appContext.getBean("stockService");
		
		StockInfoDao stockInfoDao = (StockInfoDao)appContext.getBean("stockInfoDao");
		
		List<Stock> allStk = stkService.getAvaiableStocks();
		// --- clean old data ---
		stockInfoDao.cleanAll();
		
		List<StockInfo> entities = new Vector<StockInfo>();
		
		for (Stock stk : allStk) {
			StockInfo stkInfo = new StockInfo();
			stkInfo.setName( stk.getName() );
			stkInfo.setCode( stk.getType()+stk.getCode() );
			entities.add( stkInfo );
		}
		
		stockInfoDao.saveAll(entities);
		

	}
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClassLoader clsLoader = UpdateStockInfoCapital.class.getClassLoader();
		// --- run the main page ---
		// build the read the path , dzh's home path
		String[] configContextFiles = {
				"classpath:/spring/configurationContext.xml",
				"classpath*:/spring/applicationContext.xml",
				"classpath*:/spring/dataAccessContext.xml"
		};
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configContextFiles,true);
		
	
		UpdateStockInfoCapital mainProg = new UpdateStockInfoCapital();
		
		try {
			mainProg.exec(ctx);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		logger.info("program run ... Ok!");
		



	}

}
